/**
 * 
 */
package com.java.test.platmanage.controller;

import com.alibaba.fastjson.JSONObject;
import com.java.test.activity.dto.ActivityManageInfo;
import com.java.test.controller.BaseController;
import com.java.test.platmanage.dto.GameListInfo;
import com.java.test.platmanage.dto.PlatParamInfo;
import com.java.test.platmanage.service.PlatService;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.timer.MessagePushTimer;
import com.java.test.user.dto.PlayAccountInfo;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.JsonUtil;
import com.java.test.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/platmanage")
public class PlatManageController extends BaseController {

	@Autowired
	PlatService platService;

	/**
	 * 平台参数查询
	 * 
	 * @return
	 */
	@RequestMapping("/platparamList")
	public JsonResult platparamList() {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			List<PlatParamInfo> list = platService.selectplatList();
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 添加平台参数
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/addplatparam")
	public JsonResult addplatparam(@RequestBody PlatParamInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {

			boolean isexits = platService.selectcheckdomain(info.getRealmName());

			if (isexits) {
				js.setCode(HttpErrorCode.DOMAINISEXITS.getCode());
				js.setMessage(HttpErrorCode.DOMAINISEXITS.getContent());
				return js;
			}

			boolean ptisexits = platService.selectplatname(info.getPlatName());
			if (ptisexits) {
				js.setCode(HttpErrorCode.PLATNAMEISEXITS.getCode());
				js.setMessage(HttpErrorCode.PLATNAMEISEXITS.getContent());
				return js;
			}

			int i = platService.insertplatParam(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-参数设置-添加");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 平台参数更新
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/updateplatparam")
	public JsonResult updateplatparam(@RequestBody PlatParamInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = platService.updateplatParam(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-参数设置-修改");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updateplatparamStatus")
	public JsonResult updateplatparamStatus(@RequestBody PlatParamInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = platService.updateplatparamStatus(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			//MessagePushTimer.gameALLList.add(String.valueOf(info.getId()));
			if(!MessagePushTimer.gameALLCLQ.contains(String.valueOf(info.getId()))){
				MessagePushTimer.gameALLCLQ.offer(String.valueOf(info.getId()));
			}

			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-参数设置-修改");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/********* ==========游戏列表============= ********************/

	/**
	 * 游戏列表查询
	 * 
	 * @return
	 */
	@RequestMapping("/platgameList")
	public JsonResult platgameList(@RequestBody PageInfo<GameListInfo> page) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			GameListInfo info = page.getInfo();
			if (info == null)
				info = new GameListInfo();
			page.setInfo(info);
			PageInfo<GameListInfo> list = platService.selectgameList(page);
			if (list == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 根据游戏类型查询所有游戏
	 * 
	 * @return
	 */
	@RequestMapping("/selectgamebygametype")
	public JsonResult selectgamebygametype(@RequestBody GameListInfo infos) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			List<GameListInfo> info = platService.selectgamebygametype(infos.getId());
			if (info == null || info.size() <= 0) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	/**
	 * 游戏查询
	 * 
	 * @return
	 */
	@RequestMapping("/selectgamebyId")
	public JsonResult selectgamebyId(@RequestBody GameListInfo infos) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			GameListInfo info = platService.selectgamebyId(infos.getId());
			if (info == null) {
				js.setCode(HttpErrorCode.REQUESTSUCCESS.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			js.setData(info);
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updategameInfo")
	public JsonResult updategameInfo(@RequestBody GameListInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = platService.updateGameListInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			MessagePushTimer.gameList.add(String.valueOf(info.getId()));
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-修改");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updategamesortInfo")
	public JsonResult updategamesortInfo(@RequestBody String json, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {

			List<GameListInfo> info = new ArrayList<GameListInfo>();
			JSONObject jsobj = JSONObject.parseObject(json);
			int isnotPl = jsobj.getInteger("isnotPl");
			if (isnotPl == 1) { // 批量
				String idstr = jsobj.getString("id");
				int sort = jsobj.getInteger("sort");
				String id[] = idstr.split(",");
				for (int i = 0; i < id.length; i++) {
					GameListInfo obj = new GameListInfo();
					obj.setId(Integer.valueOf(id[i]));
					obj.setSort(sort);
					info.add(obj);
				}
			} else if (isnotPl == 0) {
				GameListInfo obj = JsonUtil.json2obj(json, GameListInfo.class);
				info.add(obj);
			} else {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;

			}

			int i = platService.updateGamesortInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			info.forEach(b->{
				System.out.println(b.getId()+"ppppppppppp");
				MessagePushTimer.gameList.add(String.valueOf(b.getId()));
			});
			

			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-排序");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/addgameInfo")
	public JsonResult addgameInfo(@RequestBody GameListInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = platService.insertGameInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			MessagePushTimer.gameList.add(String.valueOf(info.getGameCode()));
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-添加");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/updategamestatusInfo")
	public JsonResult updategamestatusInfo(@RequestBody GameListInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = platService.updategamestatusInfo(info);
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			MessagePushTimer.gameList.add(String.valueOf(info.getId()));
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-排序");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}
	
	
	
	@RequestMapping("/updategamestatusInfoPL")
	public JsonResult updategamestatusInfoPL(@RequestBody Map<String,Object>  map, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			
			if(!map.containsKey("ids")) {
				js.setCode(HttpErrorCode.PARAMERROR.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			if(!map.containsKey("start")) {
				js.setCode(HttpErrorCode.PARAMERROR.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			
			@SuppressWarnings("unchecked")
			List<Integer> info = (List<Integer>) map.get("ids");
			int ids[]=info.stream().mapToInt(Integer::valueOf).toArray();
			int i = platService.updategamestatusInfoPL(ids,Integer.valueOf(map.get("start").toString()));
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			for(int j=0;j<ids.length;j++) {
				MessagePushTimer.gameList.add(String.valueOf(ids[j]));
			}
			
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-排序");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}
	
	@RequestMapping("/updategameisnothotInfoPL")
	public JsonResult updategameisnothotInfoPL(@RequestBody Map<String,Object>  map, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			
			if(!map.containsKey("ids")) {
				js.setCode(HttpErrorCode.PARAMERROR.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			if(!map.containsKey("start")) {
				js.setCode(HttpErrorCode.PARAMERROR.getCode());
				js.setMessage(HttpErrorCode.PARAMERROR.getContent());
				return js;
			}
			
			@SuppressWarnings("unchecked")
			List<Integer> info = (List<Integer>) map.get("ids");
			int ids[]=info.stream().mapToInt(Integer::valueOf).toArray();
			int i = platService.updategameisnothotInfoPL(ids,Integer.valueOf(map.get("start").toString()));
			if (i <= 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}
			for(int j=0;j<ids.length;j++) {
				MessagePushTimer.gameList.add(String.valueOf(ids[j]));
			}
			
			// log
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "平台管理-游戏列表-排序");

		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}
	
	
	
	
	
}
